<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/ajax.js"></script>
</head>

<body>

</body>
<script>
    /* // 发送请求,返回Promise实例
    var p = ajaxPromise({
        type: "get",
        url: "../data/1.txt",
        async: true,
        dataType: "text",
    })
    console.log(p);

    // 调用Promise实例的方法
    p.then(function (res) {
        console.log("fulfilled", res);
    }).catch(function (err) {
        console.log("rejected", res);
    }) */



    // Promise链式操作
    var p = ajaxPromise({
        type: "get",
        url: "../data/1.txt",
        async: true,
        dataType: "text",
    })
    console.log(p);

    // 调用Promise实例的方法
    var str = "";
    p.then(function (res) {
        console.log("fulfilled", res);
        str += res;

        return ajaxPromise({  //第一个请求成功之后,再请求第二个
            type: "get",
            url: "../data/2.txt",
            async: true,
            dataType: "text",
        })
    }).then(function (res) {
        console.log("fulfilled", res);
        str += res;
        return ajaxPromise({   //第2个请求成功之后,再请求第3个
            type: "get",
            url: "../data/3.txt",
            async: true,
            dataType: "text",
        })
    }).then(function (res) {
        console.log("fulfilled", res);
        str += res;
        return str;
    }).then(function (res) {
        console.log("三次请求成功", res);
    }).catch(function (err) {
        console.log("rejected", res);
    })



</script>

</html>